﻿using System;
using System.Xml.Linq;
using NUnit.Framework;

namespace BuzzX8.Xmpp.Test
{
    [TestFixture]
    public abstract class StreamElementTestBase
    {
        private XmlNamespace elementNamespace;
        private string elementName;

        protected StreamElementTestBase() : this("element", "xml_namespace", "pfx")
        {
        }

        protected StreamElementTestBase(string elementName, string namespaceUri, string namespaceName)
        {
            if (string.IsNullOrEmpty(elementName)) throw new ArgumentNullException("elementName");
            this.elementName = elementName;
            this.elementNamespace = new XmlNamespace(namespaceName, namespaceUri);
        }

        protected string ElementName
        {
            get { return elementName; }
        }

        protected XmlNamespace ElementNamespace
        {
            get { return elementNamespace; }
        }

        protected XElement CreateXElement(string name)
        {
            return CreateXElement(name, "", "");
        }

        protected XElement CreateXElement(string name, string namespaceUri)
        {
            return CreateXElement(name, "", namespaceUri);
        }

        protected XElement CreateXElement(string name, string namespaceName, string namespaceUri)
        {
            if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name");
            XNamespace ns = namespaceUri;
            XElement element = new XElement(ns + name);

            if (!string.IsNullOrEmpty(namespaceName))
            {
                element.SetAttributeValue(XNamespace.Xmlns + namespaceName, namespaceUri);
            }

            return element;
        }

        protected void VerifyXml(XElement expectedXml, StreamElement element)
        {
            VerifyXml(expectedXml.ToString(), element);
        }

        protected void VerifyXml(string expectedXml, StreamElement element)
        {
            XmlAssert.AreEquals(expectedXml, element.ToString());
        }
    }
}
